package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.JoinAuctionBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityPayAuctionCityBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.dialog.CommentDialog;
import com.shenzhen.horselive.ui.dialog.CommentOneButtonDialog;
import com.shenzhen.horselive.ui.dialog.InputDialog;
import com.shenzhen.horselive.utils.AnimationUtils;
import com.shenzhen.horselive.utils.BigDecimalUtils;
import com.shenzhen.horselive.utils.DialogManager;

import java.text.NumberFormat;

public class PayAuctionCityActivity extends AppBarActivity {

    ActivityPayAuctionCityBinding  binding;
    //起拍价格
    double   starrPrice;
    //最低的加价幅度
    double  minAddprice;
    //最大的限价格
    double  maxPrice;
    //最低价格
    double  minPrice;
    //最高的加价
    double  maxAddPrice;
    //最开始的当前的价格
    double currentPrice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  ActivityPayAuctionCityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleRed();
        setTitle("竞拍城市");
        initViews();
        intData();
    }
    JoinAuctionBean  bean;
    private void intData() {
        String  id =  getIntent().getStringExtra("id");
        if (id==null)
            return;

        ApiFactory.getApiService().joinAuction(User.get().getMyInfo().getToken()
        ,id).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<JoinAuctionBean>(this) {
                    @Override
                    protected void onNextInActive(JoinAuctionBean joinAuctionBean) {
                        if (joinAuctionBean.isSuccess())
                        {
                            bean =joinAuctionBean;
                            //起拍价格
                            currentPrice = Double.parseDouble(joinAuctionBean.getData().getCurrentPrice());
                            maxAddPrice =Double.parseDouble(joinAuctionBean.getData().getMaxAddPrice());
                            minPrice = Double.parseDouble(joinAuctionBean.getData().getCurrentPrice());
                            minAddprice = Double.parseDouble(joinAuctionBean.getData().getMinAddPrice());
                            maxPrice = Double.parseDouble(joinAuctionBean.getData().getMaxPrice());
                            starrPrice =Double.parseDouble(joinAuctionBean.getData().getCurrentPrice());
                            binding.tvAddr.setText("城市："+joinAuctionBean.getData().getAreaName());
                            binding.tvTotalPrice.setText(joinAuctionBean.getData().getCurrentPrice()+"蜂值");
                            binding.tvFenzhiPay.setText("支付蜂值："+starrPrice);
                            binding.tvPrice.setText(String.format("当前竞拍价%s蜂值 ",joinAuctionBean.getData().getCurrentPrice()));
                            binding.tvAddPrice.setText(String.format("每次 加价%s蜂值",joinAuctionBean.getData().getMinAddPrice()));
                        }else
                        {
                            showToast(joinAuctionBean.getErrMessage());
                        }

                    }
                });

        //查询蜂值余额
        getAcountForType(1);

    }



    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                        if (userAcountBean.isSuccess())
                        {
                            NumberFormat numberFormat = NumberFormat.getNumberInstance();
                            numberFormat.setMaximumFractionDigits(10);//最多保留10位
                            double  value = Double.parseDouble(userAcountBean.getData().getValue());
//                           //显示个数
                            binding.tvLeftMoney.setText(String.format("峰值余额:%s(蜂值)",userAcountBean.getData().getAmount()));
                        }
                    }
                });

    }

    private void initViews() {
        //增加价格
        binding.imgJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean==null)
                    return;

                //判断最大加价幅度
                if ((BigDecimalUtils.aJianB(starrPrice,currentPrice))>maxAddPrice)
                {
                    showToast("已经超过最大加价");
                    return;
                }

                    if ((BigDecimalUtils.aJiaB(starrPrice,minAddprice))>=maxPrice)
                    {
                        showToast("已经超过最大限价");
                        return;
                    }
                  starrPrice =BigDecimalUtils.aJiaB(starrPrice,minAddprice);
                  binding.tvTotalPrice.setText(starrPrice+"蜂值");
                  binding.tvFenzhiPay.setText("支付蜂值："+starrPrice);

            }
        });

        //减少价格
        binding.imgJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean==null)
                    return;;

                    //不能低于最低的价格
                if (BigDecimalUtils.aJianB(starrPrice,minAddprice)<minPrice)
                {
                    return;
                }

                starrPrice=BigDecimalUtils.aJianB(starrPrice,minAddprice);
                binding.tvTotalPrice.setText(starrPrice+"蜂值");
                binding.tvFenzhiPay.setText("支付数量："+starrPrice+"蜂值");
            }
        });



        binding.linerPayGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay();
            }
        });
    }

    private void pay() {
//        CommentOneButtonDialog  dialog = new CommentOneButtonDialog(this);
//        dialog.setTitleText("支付成功");
//        dialog.setContent("支付价格：1000.00蜂值");
//        dialog.setConfirm(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
        if (bean==null)
            return;

        if (starrPrice<=Double.parseDouble(bean.getData().getCurrentPrice()))
        {
            showToast("竞拍加价必须大于当前竞拍价");
            return;
        }

        DialogManager  manager = DialogManager.getInstance();

        manager.showInputDialog(this, "支付", "支付数量："+starrPrice+"蜂值", new InputDialog.OnInputFinsh() {
            @Override
            public void onFinish(String s) {
                 ApiFactory.getApiService().joinAuctionSure(User.get().getMyInfo().getToken()
                 ,bean.getData().getAuctionSerialNum(),starrPrice+"",s)
                         .compose(SchedulersCompat.applyIoSchedulers())
                         .safeSubscribe(new LoadingViewObserver<CommResponseBean>(PayAuctionCityActivity.this) {
                             @Override
                             protected void onNextInActive(CommResponseBean commResponseBean) {
                                 if (commResponseBean.isSuccess())
                                 {
                                     //支付成功
                                   manager.showCommentDialog(getContext(), "支付成功", "", new CommentOneButtonDialog.OneButtonClickListener() {
                                       @Override
                                       public void OnClick() {
                                           finish();
                                       }
                                   });
                                 }
                                 else
                                 {
                                     showToast(commResponseBean.getErrMessage());
                                 }
                             }
                         });
            }
        });


    }



    //参数过来
    public static void start(Context context,String  id) {
        Intent starter = new Intent(context, PayAuctionCityActivity.class);
        starter.putExtra("id",id);
        context.startActivity(starter);
    }




}